package com.huaao.micropolice.root.interceptor;

import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.MDC;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.huaao.micropolice.common.util.MD5;
import com.huaao.micropolice.core.common.Context;
import com.huaao.micropolice.core.model.system.User;
import com.huaao.micropolice.root.commons.RequestUtil;

public class LogsInterceptor implements HandlerInterceptor{

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		User user = Context.getCurrentUser();
		if(user != null){
			MDC.put("userId",user.getId());
	        MDC.put("userName", user.getUsername());
	        MDC.put("id", MD5.encryptionMD5(UUID.randomUUID().toString()));
	        MDC.put("remoteAddr", RequestUtil.getRemoteAddr(request));
	        MDC.put("uri", request.getRequestURI());
	        MDC.put("params", RequestUtil.buildParam(request));
		}
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
	}

	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
	}

}
